<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" ref="searchRef" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        stripe
        border
        :toolbar="false"
        :loadOnCreated="false"
      >
        <template #city="{ row }">
          <span class="orange">
            {{ row.tenant }}
          </span>
          <br />
          {{ row.site }}<br />
          <el-tag :type="deadType(row.dead)">
            {{ deadText(row.dead) }}
          </el-tag>
        </template>
        <template #code="{ row }">
          {{ row.id }}<br />
          {{ row.code }} <br />
          <el-tag :type="showType(row.is_display)">
            {{ showText(row.is_display) }}
          </el-tag>
        </template>
        <template #position="{ row }">
          <span @click="openJobDetail(row)" class="pointer blue">
            {{ row.title }}
          </span>
          <br />
          <el-icon :size="12" class="pointer" @click="openJobDetail(row)">
            <ZoomIn />
          </el-icon>
          <span
            @click="setCompany(row)"
            class="pointer"
            style="margin-left: 3px"
          >
            {{ row.company_name }}
          </span>
        </template>
        <template #deadline="{ row }">
          {{ row.deadline }}<br />
          {{ row.time_remaining }}<br />
          <span class="green">{{ row.admin_remark }}</span>
        </template>
        <template #workdate="{ row }">
          {{ row.work_date }}<br />
          <span v-if="row.shifts_id > 0" class="orange">{{ row.shifts }} </span>
          {{ row.work_time }}<br />
          {{ row.work_place }}
        </template>
        <template #apply="{ row }">
          {{ row.remaining_apply }}<br />
          {{ row.apply_number }}/{{ row.total_limit }}<br />
          {{ row.apply_wating }}人排队
        </template>
        <template #action="{ row }">
          <router-link
            :to="'/company/apply?id=' + row.id"
            style="text-decoration: none"
          >
            <el-link type="success" :underline="false"> 报名 </el-link>
          </router-link>
          <el-divider direction="vertical" />

          <el-link type="primary" :underline="false" @click="openSignIn(row)">
            签到表
          </el-link>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 编辑弹窗 -->
    <edit v-model="showEdit" :data="current" @done="reload" />
    <!-- 工作详情 -->
    <job-detail v-model="showJobDetail" :job-id="currentJobId" />
    <!-- 签到表 -->
    <sign-in v-model="showSignIn" :job="current" @done="reload" />
  </ele-page>
</template>

<script setup>
  import { ref } from 'vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import { ElMessageBox } from 'element-plus/es';
  import Search from './components/search.vue';
  import Edit from './components/edit.vue';
  import JobDetail from './details/index.vue';
  import SignIn from '@/views/company/apply/components/sign-in.vue';
  import {
    pageJob,
    updateJobDisplay,
    cancelJob,
    delJob,
    sendJobMpMessage
  } from '@/api/company';
  import { ZoomIn } from '@element-plus/icons-vue';

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = ref([
    {
      label: '城市',
      width: 100,
      align: 'center',
      slot: 'city'
    },
    {
      label: 'ID/编号',
      width: 150,
      align: 'center',
      slot: 'code'
    },
    {
      label: '职位/公司',
      width: 250,
      align: 'center',
      slot: 'position'
    },
    {
      label: '报名截止',
      width: 150,
      align: 'center',
      slot: 'deadline'
    },
    {
      label: '工作日期/时间',
      minWidth: 290,
      align: 'center',
      slot: 'workdate'
    },
    {
      label: '排队/报名',
      width: 120,
      align: 'center',
      slot: 'apply'
    },
    {
      columnKey: 'action',
      label: '操作',
      width: 180,
      align: 'center',
      slot: 'action'
    }
  ]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 要显示的工作 */
  const currentJobId = ref(0);

  /** 是否显示编辑弹窗 */
  const showEdit = ref(false);

  /** 工作详情弹窗 */
  const showJobDetail = ref(false);

  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageJob({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  const searchRef = ref(null);

  /** 搜索选中的公司 */
  const setCompany = (row) => {
    console.log(row.company_name, searchRef.value);
    searchRef.value.setCompanyName(row.company_name);
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 打开编辑弹窗 */
  const openEdit = (row) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  /** 打开工作详情弹窗 */
  const openJobDetail = (row) => {
    currentJobId.value = row.id ?? 0;
    showJobDetail.value = true;
  };

  const showSignIn = ref(false);
  /** 打开工作详情弹窗 */
  const openSignIn = (row) => {
    current.value = row ?? null;
    showSignIn.value = true;
  };

  const deadText = (value) => {
    const cs = {
      1: '已结束',
      0: '进行中'
    };
    return cs[value];
  };
  const deadType = (state) => {
    const stateMap = {
      1: 'info',
      0: 'success'
    };
    return stateMap[state];
  };
  const showText = (value) => {
    const cs = {
      1: '显示',
      0: '隐藏'
    };
    return cs[value];
  };
  const showType = (state) => {
    const stateMap = {
      1: 'success',
      0: 'info'
    };
    return stateMap[state];
  };

  /** 修改用户状态 */
  const handleShow = (row) => {
    const is_display = row.is_display === 1 ? 0 : 1;
    updateJobDisplay({ id: row.id, is_display })
      .then(() => {
        row.is_display = is_display;
        EleMessage.success('操作成功');
      })
      .catch((e) => {
        EleMessage.error(e.message);
      });
  };

  /** 结束工作 */
  const handleDead = (row) => {
    const rows = row == null ? selections.value : [row];
    if (!rows.length) {
      EleMessage.error('请至少选择一条数据');
      return;
    }
    ElMessageBox.confirm(
      '确定要结束“' + rows.map((d) => d.title).join(', ') + '”的工作吗?',
      '系统提示',
      { type: 'warning', draggable: true }
    )
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        const id = row.id;
        cancelJob({ id })
          .then(() => {
            loading.close();
            EleMessage.success('操作成功');
            reload();
          })
          .catch((e) => {
            loading.close();
            EleMessage.error(e.message);
          });
      })
      .catch(() => {});
  };

  /** 删除工作 */
  const handleDelete = (row) => {
    const rows = row == null ? selections.value : [row];
    if (!rows.length) {
      EleMessage.error('请至少选择一条数据');
      return;
    }
    ElMessageBox.confirm(
      '确定要删除“' + rows.map((d) => d.title).join(', ') + '”的工作吗?',
      '系统提示',
      { type: 'warning', draggable: true }
    )
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        const id = row.id;
        delJob({ id })
          .then(() => {
            loading.close();
            EleMessage.success('操作成功');
            reload();
          })
          .catch((e) => {
            loading.close();
            EleMessage.error(e.message);
          });
      })
      .catch(() => {});
  };
  /** 发短信通知 */
  const reSendMpMessage = (row) => {
    const rows = row == null ? selections.value : [row];
    if (!rows.length) {
      EleMessage.error('请至少选择一条数据');
      return;
    }
    ElMessageBox.confirm(
      '确定要重新发送“' +
        rows.map((d) => d.title).join(', ') +
        '”的工作提示给会员?',
      '系统提示',
      { type: 'warning', draggable: true }
    )
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        sendJobMpMessage({ position_id: row.position_id, id: row.id })
          .then(() => {
            loading.close();
            EleMessage.success('操作成功');
          })
          .catch((e) => {
            loading.close();
            EleMessage.error(e.message);
          });
      })
      .catch(() => {});
  };
</script>

<script>
  export default {
    name: 'JobList'
  };
</script>
